package com.tsingteng.cosfun.widget.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.tsingteng.cosfun.R;
import com.tsingteng.cosfun.utils.AppUtils;
import com.tsingteng.cosfun.utils.LogUtils;
import com.tsingteng.cosfun.utils.ToastUtils;

/**
 * 描述: 发布页面
 * 创建人: 肖利
 * 时间: 2018/8/3
 * qq: (872179966)
 */

public class VideoPulishDialog implements View.OnClickListener {
    private Activity activity;
    private Dialog dialog;
    private RelativeLayout mRlArrUp;
    private RelativeLayout mRlContent;
    private LinearLayout mLlPublish;
    private EditText mEtContent;
    private LinearLayout mLlDrafts;
    private OnVideoPublishDialogListener onVideoPublishDialogListener;

    public VideoPulishDialog(Activity activity) {
        this.activity = activity;
        init();
    }

    private void init() {
        dialog = new Dialog(activity, R.style.beautydialog);
        dialog.setContentView(R.layout.dialig_publish_video);
        mRlArrUp = dialog.findViewById(R.id.rl_arr_up);
        mRlContent = dialog.findViewById(R.id.rl_content);
        mEtContent = dialog.findViewById(R.id.et_content);
        mLlPublish = dialog.findViewById(R.id.ll_publish);
        mLlDrafts = dialog.findViewById(R.id.ll_drafts);
        mLlPublish.setOnClickListener(this);
        mLlDrafts.setOnClickListener(this);
        mRlArrUp.setOnClickListener(this);

        dialog.setOnDismissListener(dialog -> {
            if (onVideoPublishDialogListener != null) {
                onVideoPublishDialogListener.onDissMiss();
            }
        });
        mEtContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                LogUtils.e("onTextChangedcount",count+"");
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().trim().length()>=30) {
                    AppUtils.showHintDialogToast(activity,"最多可输入30个字");
                }
            }
        });
    }




    public void show() {
        Window window = dialog.getWindow();
        window.setGravity(Gravity.BOTTOM);
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        window.setWindowAnimations(R.style.dialogWindowAnim);
        mRlArrUp.setVisibility(View.VISIBLE);
        dialog.show();


    }

    public void dissMiss() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
            mRlArrUp.setVisibility(View.GONE);

        }
    }
    @Override
    public void onClick(View v) {
        if(onVideoPublishDialogListener == null) return;
        switch (v.getId()) {
            case R.id.ll_drafts:
                onVideoPublishDialogListener.onDrafts();
                break;
            case R.id.ll_publish:
                onVideoPublishDialogListener.onPublish();
                break;
            case R.id.rl_arr_up:
                dissMiss();
                break;
        }
    }

    public interface OnVideoPublishDialogListener {
        void onDissMiss();

        void onPublish();

        void onDrafts();
    }

    public void setOnVideoPublishDialogDisMissListener(OnVideoPublishDialogListener onVideoPublishDialogListener) {
        this.onVideoPublishDialogListener = onVideoPublishDialogListener;
    }

    public String getTitleText() {
        if (mEtContent!= null) {
           return mEtContent.getText().toString().trim();
        }
        return null;
    }

}
